package com.kingyun.gpsinspection.purificationservice.services.tdm.innerservice.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.EmployeeVo;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.tdm.KeyOperationMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.KeyOperation;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.KeyOperationVo;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.WorkOrder;
import com.kingyun.gpsinspection.purificationservice.facade.service.tdm.KeyOperationService;
import com.kingyun.gpsinspection.purificationservice.facade.service.tdm.WorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * Created by jiangjx on 2017/1/17.
 * 关键操作受控管理service实现
 */
public class KeyOperationServiceImpl implements KeyOperationService {

    @Autowired
    private KeyOperationMapper keyOperationMapper;

    @Autowired
    private WorkOrderService workOrderService;

    /**
     * 根据条件查询分页数据
     *
     * @param keyOperationVo 查询条件封装类
     * @return
     */
    @Override
    public JSONObject queryKeyOperationList(KeyOperationVo keyOperationVo) {
       /*装载查询条件*/
        keyOperationVo.setLimit(null == keyOperationVo.getLimit() ? 10 : keyOperationVo.getLimit());
        keyOperationVo.setOffset(null == keyOperationVo.getOffset() ? 0 : keyOperationVo.getOffset());
        JSONObject jsonObject = new JSONObject();
        /* 进行分页查询分离 */
        jsonObject.put("total", keyOperationMapper.queryCount(keyOperationVo));//查询总条数
        List<KeyOperation> baseRows = keyOperationMapper.queryTableList(keyOperationVo);//查询分页内容
        /*返回结果*/
        jsonObject.put("rows", JSON.toJSON(baseRows));
        return jsonObject;
    }

    /**
     * 修改工单状态为已审批
     *
     *
     * @param employeeVo
     * @param workOrderId 工单ID
     * @param state       审批状态
     * @return
     */
    @Override
    public Boolean updateWorkOrder(EmployeeVo employeeVo, String workOrderId, Short state) throws Exception {
        WorkOrder workOrder = workOrderService.findWorkOrderById(workOrderId);
        if (null != workOrder && workOrder.getStatus() == 3) {
            workOrder = new WorkOrder();
            /*装载要修改的数据*/
            workOrder.setWorkOrderId(workOrderId);//set工单id
            workOrder.setStatus(state);//set工单状态
            workOrder.setApprovalTime(new Date());
            int flag = workOrderService.updateByPrimaryKey(employeeVo, workOrder);//执行修改操作
            return flag > 0 ? true : false;//返回结果
        } else {
            return false;
        }
    }
}
